package com.sxm.yiwei.activitis;

import android.Manifest;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AlertDialog;
import android.support.v7.view.ContextThemeWrapper;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.tools.BaseIntents;
import com.sxm.yiwei.tools.CropPictures;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FWSellServiceActivity extends BaseActivity implements View.OnClickListener{

    private EditText editname;
    private EditText editprice;
    private RelativeLayout sellserviceexplain;
    private ImageView sellserviceback;
    private TextView sellselecttype;
    private Spinner priceType;
    private List<String> imglist = new ArrayList();
    private File mCurrentPhotoFile;
    private static final int PHOTO_TEXT = 3022;
    private static final int PHOTO_PICKED_WITH_DATA = 3021;
    /*用来标识请求照相功能的activity*/
    private static final int CAMERA_WITH_DATA = 3023;
    private static final File PHOTO_DIR = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera");
    private Bitmap photo;
    private String path1;
    private  int currentIndex =0;
    private ImageView sellimg1;
    private ImageView sellimg2;
    private ImageView sellimg3;
    private ImageView sellimg4;
    private ImageView sellimg5;
    private ImageView sellimg6;
    private TextView explaintext;
    private Button sendservicebt;
    private SharedPreferences sharedPreferences;
    private String token;
    private String userId;
    private String appId,shopid;
    private String explain1;
    private ArrayList<String> imglist1 = null;
    private String item;
    private CheckBox b1;
    private CheckBox b2;
    private CheckBox b3;
    private CheckBox b4;
    private CheckBox b5;
    private int item1;
    private CheckBox sendknow;
    private AlertDialog dialog;
    private Integer splitindex;
    private String from;
    private String id;
    private JSONArray imgarray;
    private JSONArray showarray;
    private List<String> backimglist;
    private List<String> backShowList;
    private JSONArray showImages;
    private JSONArray introImages;
    private String isInsurance;
    private BaseIntents baseIntents;
    private ArrayList<String> getintroimgs = null;
    private ImageView backmain;

    @Override
    protected void onRestart() {
        super.onRestart();
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        isInsurance = sharedPreferences.getString("isInsurance", "");
        b3.setChecked(false);
    }

    /**
     * 出售服务
     * @param savedInstanceState
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fwsell_service);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        token = sharedPreferences.getString("token", "");
        userId = sharedPreferences.getString("userId", "");
        appId = sharedPreferences.getString("appid", "");
        shopid = sharedPreferences.getString("shopid", "");
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        isInsurance = sharedPreferences.getString("isInsurance", "");

        Intent intent = getIntent();
        from = intent.getStringExtra("from");
        id = intent.getStringExtra("id");
        if (from.equals("bianji")){
            // 来自编辑 获取详情
            getServiceContent(id);
        }else {
            // 来自开店  不获取详情
        }
        item = "1";
        initFWSellServicer();
        baseIntents = new BaseIntents();
        sendknow.setChecked(true);
    }
        // 获取详情
    private void getServiceContent(String id) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETSERVICECONTENT);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);

        JSONObject getObj = new JSONObject();
        getObj.put("serviceId",id);
        params.addBodyParameter("params",getObj.toJSONString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d("tag", "onSuccess: 订单详情--"+result);
                JSONObject object = JSON.parseObject(result);
                String data = object.getString("data");
                JSONObject object1 = JSON.parseObject(data);
                String amount = object1.getString("amount");
                String id1 = object1.getString("id");
                String intro = object1.getString("intro");
                String name = object1.getString("name");
                String type = object1.getString("type");
                String promise = object1.getString("promise");
                String unit = object1.getString("unit");
                item = unit;
                showImages = object1.getJSONArray("showImages");
                introImages = object1.getJSONArray("introImages");
                backimglist = new ArrayList<String>();

                for (int i = 0; i < introImages.size(); i++) {
                    backimglist.add(String.valueOf(introImages.get(i)));
                }


                backShowList = new ArrayList<String>();
                for (int i = 0; i < showImages.size(); i++) {
                    backShowList.add(String.valueOf(showImages.get(i)));
                }
                editprice.setText(amount);
                editname.setText(name);
                explaintext.setText(intro);
                String[] split = promise.split(",");
                if (split.length!=0){
                    for (int i = 0; i < split.length; i++) {
                        splitindex = Integer.valueOf(split[i]);
                        Log.d("tt", "onSuccess: splitindex--"+splitindex);

                        switch (splitindex){
                            case 1:
                                b1.setChecked(true);
                                break;
                            case 2:
                                b2.setChecked(true);
                                break;
                            case 3:
                                b3.setChecked(true);
                                break;
                            case 4:
                                b4.setChecked(true);
                                break;
                            case 5:
                                b5.setChecked(true);
                                break;
                        }
                    }

              }else {

              }
                if (!type.equals("")){
                    Integer intType = Integer.valueOf(type);
                    switch (intType){
                        case 1:
                            sellselecttype.setText("劳务市场");
                            break;
                        case 2:
                            sellselecttype.setText("家政服务");
                            break;
                        case 3:
                            sellselecttype.setText("跑腿办事");
                            break;
                        case 4:
                            sellselecttype.setText("维修保养");
                            break;
                        case 5:
                            sellselecttype.setText("汽车服务");
                            break;
                        case 6:
                            sellselecttype.setText("线上服务");
                            break;
                        case 7:
                            sellselecttype.setText("陪玩导游");
                            break;
                        case 8:
                            sellselecttype.setText("按摩美容");
                            break;
                        case 9:
                            sellselecttype.setText("生活服务");
                            break;
                        case 10:
                            sellselecttype.setText("其他");
                            break;
                    }
                }else {
                    sellselecttype.setText("");
                }

               switch (showImages.size()){
                   case 1:
                       x.image().bind(sellimg1,InternetAddressUtis.IMG_URL+ showImages.get(0));
                       break;
                   case 2:
                       x.image().bind(sellimg1,InternetAddressUtis.IMG_URL+ showImages.get(0));
                       x.image().bind(sellimg2,InternetAddressUtis.IMG_URL+ showImages.get(1));
                       break;
                   case 3:
                       x.image().bind(sellimg1,InternetAddressUtis.IMG_URL+ showImages.get(0));
                       x.image().bind(sellimg2,InternetAddressUtis.IMG_URL+ showImages.get(1));
                       x.image().bind(sellimg3,InternetAddressUtis.IMG_URL+ showImages.get(2));
                       break;
                   case 4:
                       x.image().bind(sellimg1,InternetAddressUtis.IMG_URL+ showImages.get(0));
                       x.image().bind(sellimg2,InternetAddressUtis.IMG_URL+ showImages.get(1));
                       x.image().bind(sellimg3,InternetAddressUtis.IMG_URL+ showImages.get(2));
                       x.image().bind(sellimg4,InternetAddressUtis.IMG_URL+ showImages.get(3));
                       break;
                   case 5:
                       x.image().bind(sellimg1,InternetAddressUtis.IMG_URL+ showImages.get(0));
                       x.image().bind(sellimg2,InternetAddressUtis.IMG_URL+ showImages.get(1));
                       x.image().bind(sellimg3,InternetAddressUtis.IMG_URL+ showImages.get(2));
                       x.image().bind(sellimg4,InternetAddressUtis.IMG_URL+ showImages.get(3));
                       x.image().bind(sellimg5,InternetAddressUtis.IMG_URL+ showImages.get(4));
                       break;
                   case 6:
                       x.image().bind(sellimg1,InternetAddressUtis.IMG_URL+ showImages.get(0));
                       x.image().bind(sellimg2,InternetAddressUtis.IMG_URL+ showImages.get(1));
                       x.image().bind(sellimg3,InternetAddressUtis.IMG_URL+ showImages.get(2));
                       x.image().bind(sellimg4,InternetAddressUtis.IMG_URL+ showImages.get(3));
                       x.image().bind(sellimg5,InternetAddressUtis.IMG_URL+ showImages.get(4));
                       x.image().bind(sellimg6,InternetAddressUtis.IMG_URL+ showImages.get(5));
                       break;
               }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(FWSellServiceActivity.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initFWSellServicer() {
        backmain = (ImageView) findViewById(R.id.sellservise_backmain);

        editname = (EditText) findViewById(R.id.fw_sellservice_editname);
        editprice = (EditText) findViewById(R.id.fw_sellservice_editprice);
        sellserviceexplain = (RelativeLayout) findViewById(R.id.fw_sellservice_explain);
        sellserviceback = (ImageView) findViewById(R.id.fw_sellservice_back);
        sellselecttype = (TextView) findViewById(R.id.sell_selecttype);
        explaintext = (TextView) findViewById(R.id.sell_explaintext);
        sendservicebt = (Button) findViewById(R.id.sell_sendservicebt);
        sellimg1 = (ImageView) findViewById(R.id.fwsell_img1);
        sellimg2 = (ImageView) findViewById(R.id.fwsell_img2);
        sellimg3 = (ImageView) findViewById(R.id.fwsell_img3);
        sellimg4 = (ImageView) findViewById(R.id.fwsell_img4);
        sellimg5 = (ImageView) findViewById(R.id.fwsell_img5);
        sellimg6 = (ImageView) findViewById(R.id.fwsell_img6);
        priceType = (Spinner) findViewById(R.id.fw_sellservice_danjia);
        sendknow = (CheckBox) findViewById(R.id.sell_sendknow);


        b1 = (CheckBox) findViewById(R.id.box1);
        b2 = (CheckBox) findViewById(R.id.box2);
        b3 = (CheckBox) findViewById(R.id.box3);
        b4 = (CheckBox) findViewById(R.id.box4);
        b5 = (CheckBox) findViewById(R.id.box5);
        List<String> type = new ArrayList<>();
        type.add("元/时");
        type.add("元/天");
        type.add("元/月");
        type.add("元/单");
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(FWSellServiceActivity.this,android.R.layout.simple_spinner_item,type);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        priceType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                item = (position+1)+"";

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        priceType.setAdapter(adapter);
        editname.setGravity(Gravity.RIGHT);
        editprice.setGravity(Gravity.RIGHT);
        if (from.equals("bianji")){
            sellselecttype.setClickable(false);
            editname.setFocusable(false);
        }else {
            sellselecttype.setClickable(true);
            sellselecttype.setOnClickListener(this);
        }

        sellserviceback.setOnClickListener(this);
        sellserviceexplain.setOnClickListener(this);
        sellimg1.setOnClickListener(this);
        sellimg2.setOnClickListener(this);
        sellimg3.setOnClickListener(this);
        sellimg4.setOnClickListener(this);
        sellimg5.setOnClickListener(this);
        sellimg6.setOnClickListener(this);
        sendservicebt.setOnClickListener(this);
        b3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sellservise_backmain:
                startActivity(new Intent(FWSellServiceActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.sell_sendknow:
                if (!sendknow.isChecked()){
                    sendknow.setChecked(false);
                }
                break;
            case R.id.box3:
                if (isInsurance.equals("true")){
                    boolean checked = b3.isChecked();
                    if (checked){
                        b3.setChecked(true);
                    }else {
                        b3.setChecked(false);
                    }
                }else {
                    startActivity(new Intent(FWSellServiceActivity.this,MineMoney.class));
                }

                break;
            case R.id.fw_sellservice_back:
                finish();
                break;
            case R.id.sell_sendservicebt:
                if (item1>0){
                    String name = String.valueOf(editname.getText());
                    if (name.equals("")){
                        Toast.makeText(this, "请输入服务名称", Toast.LENGTH_SHORT).show();
                    }else {
                        String price = String.valueOf(editprice.getText());
                        if (price.equals("")){
                            Toast.makeText(this, "请输入价格", Toast.LENGTH_SHORT).show();
                        }else {
                            if (imglist1==null){
                                Toast.makeText(this, "请选择简介图片", Toast.LENGTH_SHORT).show();
                            }else {
                                if (from.equals("bianji")){// 编辑

                                    AlertDialog.Builder builder = new AlertDialog.Builder(FWSellServiceActivity.this,R.style.NoBackGroundDialog);
                                    View view = LayoutInflater.from(this).inflate(R.layout.progress_dialog, null);
                                    builder.setView(view);
                                    dialog = builder.create();
                                    dialog.show();
                                    sendService();
                                }else {// 发布
                                    if (imglist.size()==0){
                                        Toast.makeText(this, "请添加展示图片", Toast.LENGTH_SHORT).show();
                                    }else {
                                        AlertDialog.Builder builder = new AlertDialog.Builder(FWSellServiceActivity.this,R.style.NoBackGroundDialog);
                                        View view = LayoutInflater.from(this).inflate(R.layout.progress_dialog, null);
                                        ProgressBar diapro = (ProgressBar) view.findViewById(R.id.pro_dia);
                                        builder.setView(view);
                                        dialog = builder.create();
                                        dialog.show();
                                        sendService();
                                    }
                                }

                            }
                        }
                    }
                }else {
                    Toast.makeText(this, "请选择服务类别", Toast.LENGTH_SHORT).show();
                }


                break;
            case R.id.sell_selecttype:

                Intent i = new Intent(FWSellServiceActivity.this,SellServiceSelecttypeActivity.class);
                startActivityForResult(i,100);
                break;
            case R.id.fw_sellservice_explain:
                Intent in = new Intent(FWSellServiceActivity.this,SellServiceExplainActivity.class);
                if (introImages!=null&&introImages.size()>0){
                    in.putExtra("introimgs",introImages);
                }else {}
                startActivityForResult(in,101);

                break;
            case R.id.fwsell_img1:
                currentIndex=1;
                if (showImages!=null&&showImages.size()>=1){
                    removePic(String.valueOf(showImages.get(0)));
                }else {}
                doPickPhotoAction();
                break;
            case R.id.fwsell_img2:
                currentIndex=2;
                if (showImages!=null&&showImages.size()>=2){
                    removePic(String.valueOf(showImages.get(1)));
                }else {}
                doPickPhotoAction();
                break;
            case R.id.fwsell_img3:
                currentIndex=3;
                if (showImages!=null&&showImages.size()>=3){
                    removePic(String.valueOf(showImages.get(2)));
                }else {}
                doPickPhotoAction();
                break;
            case R.id.fwsell_img4:
                currentIndex=4;
                if (showImages!=null&&showImages.size()>=4){
                    removePic(String.valueOf(showImages.get(3)));
                }else {}
                doPickPhotoAction();
                break;
            case R.id.fwsell_img5:
                currentIndex=5;
                if (showImages!=null&&showImages.size()>=5){
                    removePic(String.valueOf(showImages.get(4)));
                }else {}
                doPickPhotoAction();
                break;
            case R.id.fwsell_img6:
                currentIndex=6;
                if (showImages!=null&&showImages.size()>=6){
                    removePic(String.valueOf(showImages.get(5)));
                }else {}
                doPickPhotoAction();
                break;
        }
    }
        // 发布服务
    private void sendService() {
        String url ;
        if (from.equals("bianji")){
            url = InternetAddressUtis.Url+InternetAddressUtis.SHOPEDITSERVICE;
        }else {
            url = InternetAddressUtis.Url+InternetAddressUtis.SENDSERVICE;
        }
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type","multipart/form-data");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        params.setMultipart(true);
        if (from.equals("bianji")){
            params.addBodyParameter("serviceId",id);
        }else {
            params.addBodyParameter("shopId",shopid);
        }

        params.addBodyParameter("type",String.valueOf(item1));
        params.addBodyParameter("name",String.valueOf(editname.getText()));
        params.addBodyParameter("amount",String.valueOf(editprice.getText()));
        params.addBodyParameter("unit",item);
        boolean check1 = b1.isChecked();
        boolean check2 = b2.isChecked();
        boolean check3 = b3.isChecked();
        boolean check4 = b4.isChecked();
        boolean check5 = b5.isChecked();
        List<Boolean> bbb = new ArrayList<>();
        bbb.add(check1);
        bbb.add(check2);
        bbb.add(check3);
        bbb.add(check4);
        bbb.add(check5);
        List<String> bao = new ArrayList<>();
        for (int i = 0; i < bbb.size(); i++) {
            if (bbb.get(i)==true){
                bao.add(""+(i+1));

            }
        }
        switch (bao.size()){
            case 1:
                params.addBodyParameter("promise",bao.get(0));
                break;
            case 2:
                params.addBodyParameter("promise",bao.get(0)+","+bao.get(1));
                break;
            case 3:
                params.addBodyParameter("promise",bao.get(0)+","+bao.get(1)+","+bao.get(2));
                break;
            case 4:
                params.addBodyParameter("promise",bao.get(0)+","+bao.get(1)+","+bao.get(2)+","+bao.get(3));
                break;
            case 5:
                params.addBodyParameter("promise",bao.get(0)+","+bao.get(1)+","+bao.get(2)+","+bao.get(3)+","+bao.get(4));
                break;
        }
        if (from.equals("bianji")){
            // 来自编辑服务
            params.addBodyParameter("intro", String.valueOf(explaintext.getText()));

            // 简介照片
            if (imglist1==null){
                for (int i = 0; i < backimglist.size(); i++) {
                    params.addBodyParameter("service",backimglist.get(i));
                }
            }else {
               if (getintroimgs!=null&&getintroimgs.size()>0){
                   for (int i = 0; i < getintroimgs.size(); i++) {
                       params.addBodyParameter("service",getintroimgs.get(i));
                   }
                   for (int i = 0; i < imglist1.size(); i++) {
                       params.addBodyParameter("service",new File(imglist1.get(i)));
                   }
               }else {
                   for (int i = 0; i < imglist1.size(); i++) {
                       params.addBodyParameter("service",new File(imglist1.get(i)));
                   }
               }
            }
            // 展示图片

            if (imglist.size()==0){ // 灭有编辑展示图片
                for (int i = 0; i < backShowList.size(); i++) {
                    params.addBodyParameter("illustrate",backShowList.get(i));
                }
            }else {// 编辑了展示图片
                for (int i = 0; i < backShowList.size(); i++) {
                    params.addBodyParameter("illustrate",backShowList.get(i));
                }
                for (int i = 0; i < imglist.size(); i++) {
                    params.addBodyParameter("illustrate",new File(imglist.get(i)));
                }
            }
        }else { // 来发发布服务
            params.addBodyParameter("intro",explain1);
            for (int i = 0; i < imglist.size(); i++) {
                params.addBodyParameter("illustrate",new File(imglist.get(i)));
            }
            for (int i = 0; i < imglist1.size(); i++) {
                params.addBodyParameter("service",new File(imglist1.get(i)));
            }
        }
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                dialog.dismiss();
                AlertDialog.Builder builder1 = new AlertDialog.Builder(FWSellServiceActivity.this,R.style.NoBackGroundDialog);
                View view = LayoutInflater.from(FWSellServiceActivity.this).inflate(R.layout.pick_dalog, null);
                TextView cont = (TextView) view.findViewById(R.id.content_t);
                Button surebt = (Button) view.findViewById(R.id.pick_surebt);
                builder1.setView(view);
                final AlertDialog dialog1 = builder1.create();
                if (from.equals("bianji")){
                    cont.setText("编辑成功,请等待审核");
                }else {
                    cont.setText("发布成功,请等待审核");
                }
                surebt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                        dialog1.dismiss();
                    }
                });
                dialog1.show();

            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

                ErrType.errType(FWSellServiceActivity.this,ex);
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
                dialog.dismiss();
            }
        });
    }
    // 点击删除图片
    private void removePic(String s) {
        for (int i = 0; i < backShowList.size(); i++) {
            if (s.equals(backShowList.get(i))){
                backShowList.remove(i);
            }
        }
    }
    private void doPickPhotoAction() {
        final Context context = this;
        AlertDialog.Builder b1 = new AlertDialog.Builder(context);
        View view = LayoutInflater.from(context).inflate(R.layout.picture_dialog, null);
        b1.setView(view);

        TextView camere = (TextView) view.findViewById(R.id.picdia_camere);
        TextView pic = (TextView) view.findViewById(R.id.picdia_pic);
        TextView cancel1 = (TextView) view.findViewById(R.id.picdia_cancel);

        final AlertDialog dialog1 = b1.create();
        Window window = dialog1.getWindow();
        camere.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String status= Environment.getExternalStorageState();
                if(status.equals(Environment.MEDIA_MOUNTED)){//判断是否有SD卡
                    // index=0;
                    baseIntents.doTakePhoto();// 用户点击了从照相机获取
                    dialog1.dismiss();
                }
                else{
                    Toast.makeText(context, "没有SD卡", Toast.LENGTH_SHORT).show();
                }
            }
        });
        pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                baseIntents.doPickPhotoFromGallery();// 从相册中去获取
                dialog1.dismiss();
            }
        });
        cancel1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog1.dismiss();
            }
        });
        dialog1.show();
        window.setGravity(Gravity.BOTTOM);
    }

    // 因为调用了Camera和Gally所以要判断他们各自的返回情况,他们启动时是这样的startActivityForResult
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode==101){
            if (data==null){

            }else {
                explain1 = data.getExtras().getString("explain");
                if (explain1==null){

                }else {
                    explaintext.setText(explain1);
                }

                imglist1 = data.getExtras().getStringArrayList("imglist");
                getintroimgs = data.getExtras().getStringArrayList("getintroimgs");

            }

        }else {}
        if (resultCode != RESULT_OK)
            return;
        switch (requestCode) {
            case PHOTO_PICKED_WITH_DATA: {// 调用Gallery返回的
                //final Bitmap photo = data.getParcelableExtra("data");
                Bitmap photo = data.getParcelableExtra("data");
                //  sengimg.setImageBitmap(photo);
                String s = CropPictures.saveBitmapFile(photo, baseIntents.getPhotoFileName());
                imglist.add(s);
                if (currentIndex==1){
                    sellimg1.setImageBitmap(photo);
                }else if (currentIndex==2){
                    sellimg2.setImageBitmap(photo);
                }else if (currentIndex==3){
                    sellimg3.setImageBitmap(photo);
                }else if (currentIndex==4){
                    sellimg4.setImageBitmap(photo);
                }else if (currentIndex==5){
                    sellimg5.setImageBitmap(photo);
                }else if (currentIndex==6){
                    sellimg6.setImageBitmap(photo);
                }
                break;
            }
            case CAMERA_WITH_DATA: {// 照相机程序返回的,再次调用图片剪辑程序去修剪图片
                baseIntents.doCropPhoto(baseIntents.mCurrentPhotoFile);
                break;
            }
            case PHOTO_TEXT:
                Uri data1 = data.getData();
                if (!TextUtils.isEmpty(data1.getAuthority())){
                    ContentResolver resolver = getContentResolver();
                    try {
                        photo = MediaStore.Images.Media.getBitmap(resolver, data1);
                        String[] proj = {MediaStore.Images.Media.DATA};
                        Cursor cursor = managedQuery(data1, proj, null, null, null);
                        if (cursor==null){
                            Toast.makeText(this, "没有找到图片", Toast.LENGTH_SHORT).show();
                        }else {
                            cursor.moveToFirst();
                            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                            //图片路径
                            path1 = cursor.getString(column_index);

                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else {
                    path1 = data1.getPath();
                }
                File file = new File(path1);
                baseIntents.doCropPhoto(file);
                break;
            case 100:
                String item = data.getStringExtra("item");
                item1 = 0;
                if (item.equals("劳务市场")){
                    item1 =1;
                }else if (item.equals("家政服务")){
                    item1 =2;
                }else if (item.equals("跑腿服务")){
                    item1 =3;
                }else if (item.equals("维修保养")){
                    item1 =4;
                }else if (item.equals("汽车服务")){
                    item1 =5;
                }else if (item.equals("线上服务")){
                    item1 =6;
                }else if (item.equals("陪玩导游")){
                    item1 =7;
                }else if (item.equals("按摩美容")){
                    item1 =8;
                }else if (item.equals("生活服务")){
                    item1 =9;
                }else {
                    item1 =10;
                }
                sellselecttype.setText(item);

                break;

        }

    }
}
